<?php
require_once 'phpmoko/lib/common.php';

require_once 'phpmoko/ui/Widget.php';

class SourceFormatter extends Widget
{
	static $cssAdded = false;
	private $code;
	private $language;

	function __construct($code, $language)
	{
		parent::__construct();

		$this->language = $language;
		$this->code = $code;

		self::setCSS();
	}

	static function setCSS()
	{
		if (!self::$cssAdded) {
			require_once 'phpmoko/ui/RootPanel.php';
			require_once 'phpmoko/utils/highlight_include.php';

			$root = RootPanel::get();
			$root->addCSSFile('/css/codestyle.css');

			self::$cssAdded = true;
		}
	}

	function toString()
	{
		if ($this->language == 'PHP') {
			return '<div class="code">' . highlight_string(str_replace("\t", '  ', $this->code), true) . '</div>';
		} else {
			$code = $this->code;
			$code = SyntaxHighlight($code, $this->language);
			$code = str_replace("\t", '&nbsp;&nbsp;', $code);
			$code = nl2br($code);

			return '<div class="code"><code>' . $code . '</code></div>';
		}
	}
}
?>